/*
 * Copyright 2008,2009,2010 Daniel Freitas
 * 
 * This file is part of DMF Generic DAO.
 * 
 * DMF Generic DAO is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General
 * Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 * 
 * DMF Generic DAO is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with DMF Generic DAO. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package com.googlecode.dmf.genericdao.spring.scanners;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * Scanner which scans for classes in a jar file located in the file system.
 * 
 * @author <a href="mailto:daniel.mfreitas@gmail.com">Daniel Freitas</a>
 * 
 */
public class LocalFileSystemJarScanner extends AbstractBasePackageScanner {

    /**
     * 
     * {@inheritDoc}
     * 
     * @see com.googlecode.dmf.genericdao.spring.scanners.IPackageScanner#extractResourceContainerUri(java.net.URI,
     *      java.lang.String)
     */
    public URI extractResourceContainerUri(final URI resourceUri, final String basePackage) throws URISyntaxException {
        return new URI(resourceUri.getSchemeSpecificPart().replaceAll("!." + basePackage, ""));
    }

    /**
     * 
     * {@inheritDoc}
     * 
     * @see com.googlecode.dmf.genericdao.spring.scanners.AbstractBasePackageScanner#getClassNames(java.net.URI,
     *      java.lang.String)
     */
    @Override
    protected List<String> getClassNames(final URI resourceUri, final String basePackage) throws IOException {
        ZipFile zipFile = new ZipFile(new File(resourceUri));

        Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();

        List<String> fileNames = new ArrayList<String>();

        String packageRegExp = basePackage.replaceAll("\\.", "\\\\.");
        String regExp = MessageFormat.format("({0}\\..*)\\.class", packageRegExp);
        Pattern p = Pattern.compile(regExp);

        while (zipEntries.hasMoreElements()) {
            ZipEntry entry = zipEntries.nextElement();

            String fullPath = entry.getName().replaceAll("/", "\\.");
            Matcher m = p.matcher(fullPath);

            if (m.find()) {
                fileNames.add(m.group(1));
            }
        }

        return fileNames;
    }

    /**
     * 
     * {@inheritDoc}
     * 
     * @see com.googlecode.dmf.genericdao.spring.scanners.IPackageScanner#supportsProtocol()
     */
    @Override
    public String supportsProtocol() {
        return "jar";
    }
}
